package com.lightheart.sphr.xiaobinggan.app;

import android.app.Application;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatTextView;
import android.widget.TextView;

import com.blankj.utilcode.util.Utils;
import com.guwu.common.utils.ToastUtil;
import com.hjq.bar.TitleBar;
import com.hjq.bar.initializer.LightBarInitializer;
import com.hjq.toast.ToastUtils;
import com.lenebf.android.app_dress.AppDress;
import com.lenebf.android.app_dress.color.EyeProtectionColor;
import com.lenebf.android.app_dress.color.GrayColor;
import com.lenebf.android.app_dress.color.NightColor;
import com.lightheart.sphr.xiaobinggan.blackandwhite.uitls.AppSetting;
import com.lightheart.sphr.xiaobinggan.di.component.ApplicationComponent;
import com.lightheart.sphr.xiaobinggan.di.component.DaggerApplicationComponent;
import com.lightheart.sphr.xiaobinggan.di.module.ApplicationModule;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.simple.spiderman.SpiderMan;
import com.xiasuhuei321.loadingdialog.manager.StyleManager;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

/**
 * Created by fucp on 2018-4-10.
 * Description :
 */

public class DCApplication extends Application {

    private ApplicationComponent mApplicationComponent;
    private static DCApplication mInstance;
    private static long readTime = 0;


    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        SpiderMan.init(this);
        initApplicationComponent();

        Utils.init(this);

        StyleManager s = new StyleManager();
        s.Anim(false).repeatTime(0).contentSize(-1).intercept(true);
        LoadingDialog.initStyle(s);

        ToastUtil.init(this);


        AppDress.wear(this);
        // recovery saved color
        int color = AppSetting.getDressColor(this);
        if (AppSetting.COLOR_BLACK_WHITE == color) {
            AppDress.tint(new GrayColor());
        } else if (AppSetting.COLOR_EYE_PROTECTION == color) {
            AppDress.tint(new EyeProtectionColor(0.3f));
        } else if (AppSetting.COLOR_NIGHT_MODE == color) {
            AppDress.tint(new NightColor());
        } else {
            AppDress.tint(null);
        }


        // 初始化 Toast
        ToastUtils.init(this);

        // 初始化 TitleBar
        TitleBar.setDefaultInitializer(new LightBarInitializer() {
            @Override
            protected TextView createTextView(Context context) {
                return new AppCompatTextView(context);
            }
        });


        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                //指定为经典Header，默认是 贝塞尔雷达Header
                return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @NonNull
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });

    }

    /**
     * 初始化ApplicationComponent
     */
    private void initApplicationComponent() {
        mApplicationComponent = DaggerApplicationComponent.builder()
                .applicationModule(new ApplicationModule(this))
                .build();
    }

    public ApplicationComponent getApplicationComponent() {
        return mApplicationComponent;
    }

    public static Context getAppContext() {
        return mInstance.getApplicationContext();
    }

    public static DCApplication getInstance() {
        return mInstance;
    }

    public static void setReadTime(long time){
        readTime = time;
    }
    public static long getReadTime(){
        if(readTime == 30){
            return 0;
        }else {
            return readTime;
        }
    }

}
